<?php
/*
 Plugin Name: Wordpress plugin aumonerie
Plugin URI:
Description: Administration du fonctionnement de l'aumonerie en ligne
Version: 0.1
Author: Laurent NGAKO, Bienvenu KENGNE, Jules Hervè MOUT
Author URI:
*/

/*
 Créer par laurent NGAKO (email : laurent.community@gmail.com)
Licensed Apache
*/

if (! class_exists("wp_aumonerie")){

	class wp_aumonerie{

		var  $admin_OptionName = 'wp_aumonerieOptions';
		var $userOptionsName = 'wp_aumonerieUserPtions';

		/**
		 * Constructeur
		 */
		function wp_aumonerie(){

		}

		function wp_install_aumonerie(){
			global $wpdb;
			$table_prefix = $wpdb->prefix;
			$table_name = $table_prefix.'jrepas';
			if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
			{
				//création des tables
				$sql ="CREATE TABLE `$table_name` (
				`id_repas` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
				`noms_repas` VARCHAR(255) NULL,
				`descriptions_repas` TEXT NOT NULL,
				`date_repas` DATETIME NOT NULL,
				`photos_repas` VARCHAR(255) NULL,
				PRIMARY KEY(id_repas)
				);";
				
				$table_name = $table_prefix.'jcategories';
				$sql .="CREATE TABLE `$table_name` (
				`idCategories` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
				`valeurs` VARCHAR(45) NULL,
				PRIMARY KEY(idCategories)
				);";

				$table_name = $table_prefix.'jmembres';
				$sql .="CREATE TABLE `$table_name` (
				`idMembres` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
				`adresses` VARCHAR(255) NULL,
				`telephones` VARCHAR(20) NULL,
				`formations` VARCHAR(255) NULL,
				`photos` VARCHAR(255) NULL,
				`dates_creation` DATETIME NULL,
				PRIMARY KEY(idMembres)
				);";

				$table_name = $table_prefix.'jevenements';
				$sql .="CREATE TABLE `$table_name` (
				`idEvenements` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
				`titres` VARCHAR(255) NULL,
				`descriptions` TEXT NULL,
				`dates_debut` DATETIME NULL,
				`dates_fin` DATETIME NULL,
				`categories` INTEGER UNSIGNED NULL,
				`photos` VARCHAR(255) NULL,
				PRIMARY KEY(idEvenements)
				);";
			}

			require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
			dbDelta($sql);

		}

		function wp_uninstall_aumonerie(){
			global $wpdb;
			$table_prefix = $wpdb->prefix;
			$table_name = $table_prefix.'jrepas';
			if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name)
			{
				//création de la table
				$sql = "DROP TABLE `$table_name`";
				$wpdb->query($sql);
				$table_name = $table_prefix.'jmembres';
				$sql = "DROP TABLE `$table_name`";
				$wpdb->query($sql);
				$table_name = $table_prefix.'jevenements';
				$sql = "DROP TABLE `$table_name`";
				$wpdb->query($sql);
				$table_name = $table_prefix.'jcategories';
				$sql = "DROP TABLE `$table_name`";
				$wpdb->query($sql);
			}
		}

		function addHeaderCode(){
			print "<!-- wp_aumonerie-->";
		}

		function transformContent(){

		}
		
		function initWidget(){
			register_sidebar_widget('widg_aum', array(&$this, 'widget_events'));
		}
		
		function widget_events(){
			
			include('widget/widget_evenements.php');
		}

		/**
		 * Recupération des options
		 */

		function getAdminOptions(){
			$wp_aumonerieAdminOptions = array(
					'enabled' => 'true',
					'exclude_ip'=>''
			);

			$wp_aumonerieOptions = get($this->admin_OptionName);
			if(!empty($wp_aumonerieOptions)){

				foreach ($wp_aumonerieOptions as $key => $option){
					$wp_aumonerieAdminOptions[$key] = $option;
				}
			}
			update_option($this->admin_OptionName,$wp_aumonerieAdminOptions);
			return $wp_aumonerieAdminOptions;
		}

		/**
		 * Renvoi le tableau des options utilisateur
		 * @global string $user_email
		 * @return string
		 */
		function getUserOptions() {
			global $user_email;
			if (empty($user_email)) {
				get_currentuserinfo();
			}
			if (empty($user_email)) {
				return '';
			}
			$options = get_option($this->userOptionsName);
			if (!isset($options)) {
				$options = array();
			}
			if (empty($options[$user_email])) {
				$options[$user_email] = 'true,true';
				update_option($this->userOptionsName, $options);
			}
			return $options;
		}

		function  init(){
			$this->getAdminOptions();
			$this->getUserOptions();
		}

		function printAdminPage() {
			// 			$options = $this->getAdminOptions();
			// 			if(isset($_POST['update_wp_aumonerie'])){
			// 				if (isset($_POST['enabled'])){
			// 					$options['enabled'] = $_POST['enabled'];
			// 				}
			// 				if(isset($_POST['exclude_ips'])){
			// 					$options['exclude_ips'] = $_POST['exclude_ips'];
			// 				}
			// 				update_option($this->adminOptionsName, $options);
			// 				print '<div class="updated"><p><strong>';
			// 				_e("Param�tres mis � jour", "wpaumonerie");
			// 				print '</strong></p></div>';
			// 			}
			include('php/admin_settings.php'); // inclusion du formulaire d'administration
		}

		/**
		 * Affichage de la configuration utilisateur du plugin
		 * @global string $user_email
		 */
		function printMembersSetting()
		{
			// 			global $user_email;
			// 			if(!is_user_logged_in()) die();
			// 			if (empty($user_email))
			// 			{
			// 				get_currentuserinfo();
			// 			}
			// 			$options = $this->getUserOptions();
			// 			if (isset($_POST['update_wp_aumonerie']) && isset($_POST['enable_chat']) && isset($_POST['enable_smileys'])) {
			// 				if (isset($user_email))
			// 				{
			// 					$options[$user_email] = $_POST['enable_chat'] . "," . $_POST['enable_smileys'];
			// 					print '<div class="updated"><p>';
			// 					_e("Paramètres mis à jour", "wpjschat");
			// 					print '</strong></p></div>';
			// 					update_option($this->userOptionsName, $options);
			// 				}
			// 			}
			// 			$opts = explode(",", $options[$user_email]);
			// 			$enable_chat = 'true';
			// 			$enable_smileys = 'true';
			// 			if (sizeof($opts) >= 2)
			// 			{
			// 				$enable_chat = $opts[0];
			// 				$enable_smileys = $opts[1];
			// 			}
			include('php/members_settings.php');
		}

	}//fin de la class
}

if(class_exists("wp_aumonerie")){
	$inst_aumonerie = new wp_aumonerie();
}
//liste des fonctions permettant d'initialiser le plugin

if (!function_exists("wp_aumonerie_settings"))
{
	function wp_aumonerie_settings() {
		global $inst_aumonerie;
		if (!isset($inst_aumonerie)) {
			return;
		}
		if (function_exists('add_menu_page'))
		{
			add_menu_page('Jer\'Aum', 'Jer\'Aum', 'administrator', 'wp_admin_aumonerie', array(&$inst_aumonerie, 'printAdminPage'), plugins_url( 'img/icon.png', __FILE__ ), 1002);
		}
	}
}

if(!function_exists("wp_aumonerie_inter"))
{
	function wp_aumonerie_inter(){

	}
}
if(!function_exists("wp_aumonerie_enqueueScript")){
	function wp_aumonerie_enqueueScript(){

	}
}
if(! function_exists("wp_aumonerie_plugin_init")){
	function  wp_aumonerie_plugin_init(){

	}
}
if(! function_exists("wp_aumonerie_admin_error")){
	function  wp_aumonerie_admin_error(){

	}
}
if(! function_exists("wp_aumonerie__post_type_tevenement")){
	function  wp_aumonerie__post_type_tevenement(){

	}
}
if(! function_exists("wp_aumonerie_add_admin_script")){
	function  wp_aumonerie_add_admin_script(){
		
	}
}
if(! function_exists("wp_aumonerie_admin_head")){
	function  wp_aumonerie_admin_head(){
		global $wp_version;
		//wp_register_style('aumonerieStylesheet', plugins_url( 'css/aumonerie_stylesheet.css', __FILE__ ) );
		//wp_enqueue_style( 'aumonerieStylesheet' );
		wp_enqueue_script( 'jquery' );
	}
}
if(! function_exists("wp_aumonerie_head")){
	function  wp_aumonerie_head(){
      
	}
}
if(! function_exists("wp_aumonerie_add_for_ios")){
	function  wp_aumonerie_add_for_ios(){

	}
}


//liste des filtres et actions
if(isset($inst_aumonerie)){
	if(function_exists('register_activation_hook'))
		register_activation_hook(__FILE__, array(&$inst_aumonerie, 'wp_install_aumonerie'));
	if(function_exists('register_deactivation_hook'))
		register_deactivation_hook(__FILE__, array(&$inst_aumonerie, 'wp_uninstall_aumonerie'));
	add_action('admin_menu', 'wp_aumonerie_settings');
	add_action("plugins_loaded", array(&$inst_aumonerie, 'initWidget'));
	add_action( 'init', 'wp_aumonerie_plugin_init' );//initialisation du plugin, notamment l'internationalisation
	//add_action( 'init', 'wp_aumonerie__post_type_tevenement' ); // register post type
	//add_action( 'admin_init', 'wp_aumonerie_admin_error' );
	add_action( 'admin_head', 'wp_aumonerie_add_admin_script' );
	add_action( 'admin_enqueue_scripts', 'wp_aumonerie_admin_head' );
	//add_action( 'wp_enqueue_scripts', 'wp_aumonerie_head' );
	add_action( 'wp_head', 'wp_aumonerie_add_for_ios' );
}